//
//  JEHManagementViewController.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/11.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHManagementViewController.h"

@interface JEHManagementViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, removeCell>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) JEHDataBaseHandle *handle;
@property (nonatomic, retain) NSMutableArray *nameArray;
@property (nonatomic, retain) NSUserDefaults *user;

@end

@implementation JEHManagementViewController

- (void)dealloc {
    
    [_user release];
    [_handle release];
    [_nameArray release];
    [_collectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self createCollectionView];
    [self createHeaderView];
    [self getNameArrayFromDatabaseHandle];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self.view nightMode];
    [self.collectionView nightMode];
    self.hidesBottomBarWhenPushed = YES;
}

#pragma mark - getNameArray
- (void)getNameArrayFromDatabaseHandle {
    
    self.nameArray = [NSMutableArray arrayWithCapacity:0];
    self.handle = [JEHDataBaseHandle shareDataBaseHandle];
    [self.handle openDataBase];
    [self.handle selectTitleArrayAllFromDataBase];
#warning self.handle.nameArray被释放掉了以后，该页的self.nameArray就会成为野指针，so用copy
    self.nameArray = self.handle.nameArray.copy;
    [self.handle closeDataBase];
}

#pragma mark - createHeaderView
- (void)createHeaderView {
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 2 * kHeaderHeight + 30)];
    [self.view addSubview:headerView];
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeaderHeight + 19, headerView.width, 1)];
    topView.backgroundColor = [UIColor grayColor];
    topView.alpha = 0.3;
    [headerView addSubview:topView];
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 2 * kHeaderHeight + 29, headerView.width, 1)];
    footerView.backgroundColor = [UIColor grayColor];
    footerView.alpha = 0.3;
    [headerView addSubview:footerView];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(self.view.width / 2 - 20, 20, 40, kHeaderHeight)];
    title.text = @"更多";
    [title nightMode];
    [headerView addSubview:title];
    
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, kHeaderHeight + 20 + 15, 70, 20)];
    leftLabel.text = @"频道管理";
    [leftLabel nightMode];
    [headerView addSubview:leftLabel];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, leftLabel.bottom + 4, leftLabel.width, 1)];
    lineView.backgroundColor = [UIColor redColor];
    [headerView addSubview:lineView];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    rightButton.frame = CGRectMake(headerView.width - 40, 35 + kHeaderHeight, 30, kHeaderHeight - 10);
    [rightButton setTitle:@"完成" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:rightButton];
}

#pragma mark - rightButtonAction
- (void)rightButtonAction {
    
    JEHMoreViewController *more = [self.navigationController.viewControllers objectAtIndex:1];
    [self.navigationController popToViewController:more animated:YES];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"ReloadData" object:nil];
}

#pragma mark - createCollectionView
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.width - 50) / 3 + 10, 30 + 10);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 2 * kHeaderHeight + 30 + 10, self.view.width - 20 + 10, self.view.height - 80 + 10) collectionViewLayout:layout];
    [_collectionView release];
    [layout release];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[JEHManagementCollectionViewCell class] forCellWithReuseIdentifier:@"JEHManagementCollectionViewCellIdentifier"];
    [self.view addSubview:self.collectionView];
}

#pragma mark - UICollectionViewDelegateMethod
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.nameArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    JEHManagementCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JEHManagementCollectionViewCellIdentifier" forIndexPath:indexPath];
    if (_nameArray && indexPath.item < self.nameArray.count) {
        
        cell.titleLabel.text = self.nameArray[indexPath.item];
        cell.temp = indexPath.item;
        if (indexPath.row == 0) {
            
            cell.titleLabel.textColor = [UIColor grayColor];
            cell.button.alpha = 0;
            cell.button.userInteractionEnabled = NO;
        } else {
            cell.titleLabel.textColor = [UIColor blackColor];
            cell.button.alpha = 1;
            cell.button.userInteractionEnabled = YES;
        }
    }
    cell.delegate = self;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
}

#pragma mark - removeCell
- (void)removeCellWithTemp:(NSInteger)temp {
    
    self.handle = [JEHDataBaseHandle shareDataBaseHandle];
    [self.handle openDataBase];
    [self.handle deleteTitleArrayFromDataBase:self.nameArray[temp]];
    [self.handle closeDataBase];
    [self getNameArrayFromDatabaseHandle];
    [self.collectionView reloadData];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"取消订阅成功" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [alert show];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [alert dismissWithClickedButtonIndex:0 animated:NO];
    });
    [alert release];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"ReloadData" object:nil userInfo:nil];
}

@end
